/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.eighteen12.web.controller.vo;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.Filter;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Logger;
/**
 *
 * @author Arif
 */
public class Utilities {
    private static final Logger LOG = Logger.getLogger(Utilities.class.getName());

    public void storeToDb(CurrencyVo result) {
        try {
            DatastoreService service = DatastoreServiceFactory.getDatastoreService();
            Key key = KeyFactory.createKey("Currency", result.bankCode);
            Entity entity = loadEntity(result.bankCode);
            if(entity==null){
                entity = new Entity("Currencies", key);
            }
            entity.setProperty("bankCode", result.bankCode);
            entity.setProperty("bankName", result.bankName);
            entity.setProperty("dollarBeli", result.dollarBeli);
            entity.setProperty("dollarJual", result.dollarJual);
            entity.setProperty("lastUpdate", result.lastUpdate);
            
            service.put(entity);
        } catch (Exception e) {
            LOG.severe(e.getMessage());
            LOG.info("Ignoring saving data to db");
        }
    }
    
    public List<CurrencyVo> loadAll(String sortBy,Boolean ascending){
        List<CurrencyVo> currencies = new ArrayList();
        try{
        Query query = new Query("Currencies")
                .addSort(sortBy,ascending?
                        Query.SortDirection.ASCENDING:
                        Query.SortDirection.DESCENDING);
        DatastoreService service = DatastoreServiceFactory.getDatastoreService();
        Iterator<Entity> entities = service.prepare(query).asIterator();
        
        
        while(entities.hasNext()){
            Entity entity = entities.next();
            CurrencyVo currencyVo = new CurrencyVo();
            currencyVo.bankCode = (String)entity.getProperty("bankCode");
            currencyVo.bankName = (String)entity.getProperty("bankName");
            currencyVo.dollarBeli = (Double)entity.getProperty("dollarBeli");
            currencyVo.dollarJual = (Double)entity.getProperty("dollarJual");
            currencyVo.lastUpdate = (Date)entity.getProperty("lastUpdate");
            currencies.add(currencyVo);
        }
        }catch(Exception e){
            LOG.severe(e.getMessage());
        }
        return currencies;
    }
    
    public Entity loadEntity(String bankCode){
        Filter bankCodeFilter = new Query.FilterPredicate("bankCode", Query.FilterOperator.EQUAL, bankCode);
        Query query = new Query("Currencies").setFilter(bankCodeFilter);
        DatastoreService service = DatastoreServiceFactory.getDatastoreService();
        Iterator<Entity> entities = service.prepare(query).asIterator();
        if(entities.hasNext()){
            return entities.next();
        }
        return null;
    }
}
